package JVM;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

//死锁简单demo
public class DeadLockDemo {

    public static void main(String[] args) {
        Lock lock1 = new ReentrantLock();
        Lock lock2 = new ReentrantLock();
        new Thread(()->{
            try {
                lock1.lock();
                Thread.sleep(100);
                lock2.lock(); //获取lock2失败，lock2被myThread2持有
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"myThread1").start();

        new Thread(()->{
            try {
                lock2.lock();
                Thread.sleep(100);
                lock1.lock(); //获取lock1失败，lock1被myThread1持有
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"myThread2").start();
    }
}
